package com.dukun.mybatis.convert;

import com.dukun.mybatis.annotation.ToEntity;
import com.dukun.mybatis.entity.Car;
import com.dukun.mybatis.entity.Person;
import com.dukun.mybatis.entity.Student;
import com.dukun.mybatis.entity.dto.CarDto;
import com.dukun.mybatis.entity.dto.PersonDto;
import com.dukun.mybatis.entity.dto.StudentDto;
import com.dukun.mybatis.entity.vo.StudentVo;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;

/**
 * Student 实体类转换器
 *
 * @author: dukun
 * date: 2021/11/19
 */
@Mapper(componentModel = "default", injectionStrategy = InjectionStrategy.FIELD)
public interface StudentConverter {

    StudentConverter INSTANCE = Mappers.getMapper(StudentConverter.class);

    /**
     * 使用了@BeanMapping之后，所有的属性都必须通过@Mapping来指定映射
     *
     * @param student 学生类实体
     * @return 学生类传输层实体
     */
    @Mapping(source = "person", target = ".")
    @Mapping(target = "price", source = "price", numberFormat = "$#.00")
    @Mapping(source = "date", dateFormat = "yy-MM-dd hh:mm:ss", target = "date")
    StudentDto studentToStudentDto(Student student);

    /**
     * 实体类-->vo
     *
     * @param student student
     * @return StudentVo
     */
    @ToEntity
    StudentVo studentToStudentVo(Student student);

    /**
     * 更新实体类
     *
     * @param studentDto StudentDto
     * @param student    student
     */
    void updateStudentFromStudentDto(StudentDto studentDto, @MappingTarget Student student);

    /**
     * Person --> PersonDto
     *
     * @param person Person
     * @return PersonDto
     */
    PersonDto personToPersonDto(Person person);

    /**
     * Car ---> CarDto
     *
     * @param car Car
     * @return CarDto
     */
    CarDto carToCarDto(Car car);
}
